---@diagnostic disable: trailing-space
local desc_new_role_mode = [[

# 新身份模式简介


【常备主】主公选将时，选将框增加4个拥有主公技的武将（至多20个）

【忠臣加强】忠臣选将时，选将框增加2个与主公势力相同的武将


主公选将后会在“左侧消息栏”报告主公技能


人数不足5人时，主公也具有主公技


*因为不是真的“身份模式”，和身份有关的技能可能出错，欢迎上报。


]]

--建议建房间前在拓展包设置里勾选“新身份模式卡牌”。如果未勾选，会自动给牌堆塞入一张【木牛流马】（方片 5）


-- 牌堆黑名单
local drawpile_blacklist = {}


local new_role_mode_getLogic = function()
  local new_role_mode_logic = GameLogic:subclass("new_role_mode_logic")

  function new_role_mode_logic:chooseGenerals()
    local room = self.room ---@ type Room
    local generalNum = room.settings.generalNum
    local n = room.settings.enableDeputy and 2 or 1
    local lord = room:getLord()---@ type ServerPlayer

    if lord ~= nil then
      room.current = lord

      local generals = room:getNGenerals(generalNum)

      local all_lords = table.filter(room.general_pile, function (name)
        return table.find(Fk.generals[name]:getSkillNameList(true), function(s)
          return Fk.skills[s].lordSkill
        end)
      end)
      if #all_lords > 0 then
        local choose_lords = table.random(all_lords, math.min(4, 20 - generalNum))
        for _, g in ipairs(choose_lords) do
          table.insert(generals, g)
        end
      end

      local lord_generals = room:askForGeneral(lord, generals, n)
      local lord_general, deputy
      if type(lord_generals) == "table" then
        deputy = lord_generals[2]
        lord_general = lord_generals[1]
      else
        lord_general = lord_generals
        lord_generals = {lord_general}
      end

      room:returnToGeneralPile(generals)
      room:findGeneral(lord_general)
      if deputy and deputy ~= "" then
        room:findGeneral(deputy)
      end

      room:prepareGeneral(lord, lord_general, deputy, true)
      room:askForChooseKingdom({lord})

      room:doBroadcastNotify("ShowToast", Fk:translate("lord_skill_intro"))
      local snames = Fk.generals[lord.general]:getSkillNameList(true)
      if Fk.generals[lord.deputyGeneral] then
        table.insertTableIfNeed(snames, Fk.generals[lord.deputyGeneral]:getSkillNameList(true))
      end
      for i, sname in ipairs(snames) do
        room:doBroadcastNotify("AddSkill", json.encode{ lord.id, sname })
        room:sendLog{ type = "#ShowLordSkill", arg = Fk:translate(sname), arg2 = Fk:translate(":"..sname), arg3 = tostring(i) }
      end

    end

    local nonlord = room:getOtherPlayers(lord, true)
    local generals = room:getNGenerals(#nonlord * generalNum)
    table.shuffle(generals)
    local loyalist_generals = table.filter(room.general_pile, function (name)
      local g = Fk.generals[name]
      return g.kingdom == lord.kingdom or g.subkingdom == lord.kingdom
    end)
    for i, p in ipairs(nonlord) do
      local arg = table.slice(generals, (i - 1) * generalNum + 1, i * generalNum + 1)
      if p.role == "loyalist" then
        -- 给忠臣添加2个与主公同势力的武将
        for _ = 1, 2 do
          if #loyalist_generals > 0 then
            table.insert(arg, table.remove(loyalist_generals, 1))
          end
        end
      end
      p.request_data = json.encode{ arg, n }
      p.default_reply = table.random(arg, n)
    end

    room:notifyMoveFocus(nonlord, "AskForGeneral")
    room:doBroadcastRequest("AskForGeneral", nonlord)

    room:returnToGeneralPile(generals)
    for _, p in ipairs(nonlord) do
      local general, deputy
      if p.general == "" and p.reply_ready then
        local general_ret = json.decode(p.client_reply)
        general = general_ret[1]
        deputy = general_ret[2]
      else
        general = p.default_reply[1]
        deputy = p.default_reply[2]
      end
      room:findGeneral(general)
      room:findGeneral(deputy)
      room:prepareGeneral(p, general, deputy)
      p.default_reply = ""
    end

    room:askForChooseKingdom(nonlord)
  end


  local addGeneralSkills = function(room, p)
    local addRoleModSkills = function(player, skillName)
      local skill = Fk.skills[skillName]
      if skill.lordSkill and player.role ~= "lord" then
        return
      end
      if #skill.attachedKingdom > 0 and not table.contains(skill.attachedKingdom, player.kingdom) then
        return
      end
      room:handleAddLoseSkills(player, skillName, nil, false)
    end
    local skills = Fk.generals[p.general].skills
    for _, s in ipairs(skills) do
      addRoleModSkills(p, s.name)
    end
    for _, sname in ipairs(Fk.generals[p.general].other_skills) do
      addRoleModSkills(p, sname)
    end
    local deputy = Fk.generals[p.deputyGeneral]
    if deputy then
      skills = deputy.skills
      for _, s in ipairs(skills) do
        addRoleModSkills(p, s.name)
      end
      for _, sname in ipairs(deputy.other_skills) do
        addRoleModSkills(p, sname)
      end
    end
  end

  function new_role_mode_logic:attachSkillToPlayers() -- 增加技能
    local room = self.room

    for _, p in ipairs(room.players) do
      addGeneralSkills (room, p)
    end
  end

  return new_role_mode_logic
end


local new_role_mode_rule = fk.CreateTriggerSkill{
  name = "#new_role_mode_rule",
  priority = 0.001,
  refresh_events = {},
  can_refresh = function(self, event, target, player, data)
    return false
  end,
  on_refresh = function(self, event, target, player, data)
    return false
  end,
}

Fk:addSkill(new_role_mode_rule)


local new_role_mode = fk.CreateGameMode{
  name = "new_role_mode",
  minPlayer = 2,
  maxPlayer = 8,
  rule = new_role_mode_rule,
  logic = new_role_mode_getLogic,
  is_counted = function(self, room)
    return #room.players >= 5
  end,
  surrender_func = function(self, playedTime)
    local roleCheck = false
    local roleText = ""
    local roleTable = {
      { "lord" },
      { "lord", "rebel" },
      { "lord", "rebel", "renegade" },
      { "lord", "loyalist", "rebel", "renegade" },
      { "lord", "loyalist", "rebel", "rebel", "renegade" },
      { "lord", "loyalist", "rebel", "rebel", "rebel", "renegade" },
      { "lord", "loyalist", "loyalist", "rebel", "rebel", "rebel", "renegade" },
      { "lord", "loyalist", "loyalist", "rebel", "rebel", "rebel", "rebel", "renegade" },
    }

    roleTable = roleTable[#Fk:currentRoom().players]

    if Self.role == "renegade" then
      local rebelNum = #table.filter(roleTable, function(role)
        return role == "rebel"
      end)

      for _, p in ipairs(Fk:currentRoom().players) do
        if p.role == "rebel" then
          if not p.dead then
            break
          else
            rebelNum = rebelNum - 1
          end
        end
      end

      roleCheck = rebelNum == 0
      roleText = "left lord and loyalist alive"
    elseif Self.role == "rebel" then
      local rebelNum = #table.filter(roleTable, function(role)
        return role == "rebel"
      end)

      local renegadeDead = not table.find(roleTable, function(role)
        return role == "renegade"
      end)
      for _, p in ipairs(Fk:currentRoom().players) do
        if p.role == "renegade" and p.dead then
          renegadeDead = true
        end

        if p ~= Self and p.role == "rebel" then
          if not p.dead then
            break
          else
            rebelNum = rebelNum - 1
          end
        end
      end

      roleCheck = renegadeDead and rebelNum == 1
      roleText = "left one rebel alive"
    else
      if Self.role == "loyalist" then
        return { { text = "loyalist never surrender", passed = false } }
      else
        if #Fk:currentRoom().alive_players == 2 then
          roleCheck = true
        else
          local lordNum = #table.filter(roleTable, function(role)
            return role == "lord" or role == "loyalist"
          end)

          local renegadeDead = not table.find(roleTable, function(role)
            return role == "renegade"
          end)
          for _, p in ipairs(Fk:currentRoom().players) do
            if p.role == "renegade" and p.dead then
              renegadeDead = true
            end

            if p ~= Self and (p.role == "lord" or p.role == "loyalist") then
              if not p.dead then
                break
              else
                lordNum = lordNum - 1
              end
            end
          end

          roleCheck = renegadeDead and lordNum == 1
        end
      end

      roleText = "left you alive"
    end

    return {
      { text = roleText, passed = roleCheck },
    }
  end,
}


Fk:loadTranslationTable{
  ["new_role_mode"] = "新身份模式",
  [":new_role_mode"] = desc_new_role_mode,
  ["#new_role_mode_rule"] = "新身份模式规则",
  ["lord_skill_intro"] = "主公技能请在左侧消息栏查看",
  ["#ShowLordSkill"] = "主公技能[%arg3]【%arg】：%arg2",
}






















return new_role_mode
